package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.sms.entity.YytSmsCoupon;
import com.ruoyi.common.sms.entity.YytSmsCouponHistory;
import com.ruoyi.common.sms.entity.YytSmsCouponSpuRelation;
import com.ruoyi.common.sms.service.YytSmsCouponHistoryService;
import com.ruoyi.common.sms.service.YytSmsCouponService;
import com.ruoyi.common.sms.service.YytSmsCouponSpuRelationService;
import com.ruoyi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
* <p>
    * 优惠券信息 前端控制器
    * </p>
*
* @author MrPing
* @since 2022-05-31
*/
@RestController
@RequestMapping("/coupon")
public class YytSmsCouponController {

    @Autowired
    private YytSmsCouponService yytSmsCouponService;

    @Autowired
    private YytSmsCouponHistoryService yytSmsCouponHistoryService;

    @Autowired
    private YytSmsCouponSpuRelationService yytSmsCouponSpuRelationService;

    /**
     * 根据id获取优惠券信息
     * @param id
     * @return
     */
    @GetMapping("/getCouponById")
    public ResponseResult getCouponById(@RequestParam("id") String id){
        return ResponseResult.success(yytSmsCouponService.getById(id));
    }

    /**
     * 用户领取分享优惠券
     * @param yytSmsCouponHistory
     * @return
     */
    @PostMapping("/userGetCoupon")
    public ResponseResult userGetCoupon(@RequestBody YytSmsCouponHistory yytSmsCouponHistory){
        QueryWrapper<YytSmsCouponHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", yytSmsCouponHistory.getMemberId()).eq("coupon_id", yytSmsCouponHistory.getCouponId());
        if(yytSmsCouponHistory.getPerLimit() <= yytSmsCouponHistoryService.count(queryWrapper)){
            return ResponseResult.success(false);
        }
        yytSmsCouponHistory.setCreateTime(LocalDateTime.now());
        UpdateWrapper<YytSmsCoupon> wrapper = new UpdateWrapper<>();
        wrapper.setSql("num = num - 1, receive_count = receive_count + 1").eq("id", yytSmsCouponHistory.getCouponId()).gt("num", 0);
        boolean update = yytSmsCouponService.update(wrapper);
        if(update){
            yytSmsCouponHistoryService.save(yytSmsCouponHistory);
        }
        return ResponseResult.success(update);
    }


    /**
     * 获取用户优惠券列表
     * @param memberId
     * @param useType
     * @return
     */
    @GetMapping("/getMyCouponList")
    public ResponseResult getMyCouponList(@RequestParam("memberId") String memberId, @RequestParam("useType") Integer useType){
        return ResponseResult.success(yytSmsCouponService.getMyCouponList(memberId, useType));
    }


    /**
     * 根据优惠券 id 获取可使用的商品列表
     * @param couponId
     * @return
     */
    @GetMapping("/getSkuByIdList")
    public ResponseResult getSkuByIdList(@RequestParam("couponId") String couponId){
        return ResponseResult.success(yytSmsCouponService.getSkuListByCouponId(couponId));
    }

}
